layui.use(['form','tree','element','table','laytpl','treeGrid','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;

    //获取路径参数
    var request = GetRequest();

    //生产管理
    var height = $(document).height();
    $("#table-content").css("height",(height - 40) + "px");
    $(window).on("resize",function () {
        $(".table-content").css('height',(height - 40) + "px");
    });

    if (request.selected == null) { //首次加载
        $.get("/process/tree/" + request.processTemplateId,function (data) {
            $.each(data.data,function (i,ele) {
                var lihtml = '<li class="layui-nav-item layui-nav-itemed">' +
                    '<a id="'+ ele.id +'" href="/admin/traceability/record/recordList2?selected='+ ele.id +'&id=' + request.id + '&processTemplateId=' + request.processTemplateId + '&tableType='+ ele.tableType +'"><span>'+ ele.name +'</span></a>';

                //如果存在二级分类
                if (ele.children != null && ele.children.length != 0) {
                    lihtml = '<li class="layui-nav-item layui-nav-itemed">' +
                        '<a href="javascript:;"><span>'+ ele.name +'</span></a><dl class="layui-nav-child">';
                    $.each(ele.children,function (j, ele) {
                        lihtml += '<dd class=""><a id="'+ ele.id +'"  href="/admin/traceability/record/recordList2?selected='+ ele.id +'&id=' + request.id + '&processTemplateId=' + request.processTemplateId + '&tableType='+ ele.tableType +'"><span class="span-width"></span>'+ ele.name +'</a></dd>';

                        //默认加载第一个过程的记录表格
                        if (j === 0 && i === 0) {
                            $("#table-list").html('');
                            $("#table-content").html('');
                            var tableArr = ele.tableType.split(",");
                            $.each(tableArr,function (index, table) {
                                if (index === 0) {
                                    var contentHtml = '';
                                    if (table == 'invest') {
                                        $("#table-list").append('<li class="layui-this">投入品记录</li>');
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                    } else if (table == 'check') {
                                        $("#table-list").append('<li class="layui-this">检查记录</li>');
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                    } else if (table == 'machining') {
                                        $("#table-list").append('<li class="layui-this">加工记录</li>');
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                    } else if (table == 'transport') {
                                        $("#table-list").append('<li class="layui-this">运输记录</li>');
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                    } else {
                                        $.ajax({
                                            url: '/table/' + table,
                                            type: 'get',
                                            async: false,
                                            success: function (data) {
                                                $("#table-list").append('<li class="layui-this">'+ data.nameZh +'</li>');
                                            }
                                        });
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecord?tableName='+ table +'&id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                        /*$.get('/table/' + table,function (data) {
                                            $("#table-list").append('<li class="layui-this">'+ data.nameZh +'</li>');
                                        });
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecord?tableName='+ table +'&id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';*/
                                    }
                                    $("#table-content").append(contentHtml);
                                } else {
                                    var contentHtml = '';
                                    if (table == 'invest') {
                                        $("#table-list").append('<li class="">投入品记录</li>');
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                    } else if (table == 'check') {
                                        $("#table-list").append('<li class="">检查记录</li>');
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                    } else if (table == 'machining') {
                                        $("#table-list").append('<li class="">加工记录</li>');
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                    } else if (table == 'transport') {
                                        $("#table-list").append('<li class="">运输记录</li>');
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                    } else {
                                        $.ajax({
                                            url: '/table/' + table,
                                            type: 'get',
                                            async: false,
                                            success: function (data) {
                                                $("#table-list").append('<li class="layui-this">'+ data.nameZh +'</li>');
                                            }
                                        });
                                        contentHtml = '<div class="layui-tab-item layui-show">' +
                                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecord?tableName='+ table +'&id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                            '</div>';
                                    }
                                    $("#table-content").append(contentHtml);
                                }

                            });

                            //添加默认环境监控设备设备
                            $("#table-list").append('<li class="">环境监控数据</li>');
                            var contentHtml = '<div class="layui-tab-item">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecordMonitor?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                '</div>';
                            $("#table-content").append(contentHtml);
                        }
                    });
                } else {
                    //默认加载第一个过程的记录表格
                    if (i === 0) {

                        var tableArr = ele.tableType.split(",");
                        $.each(tableArr,function (index, table) {
                            if (index === 0) {
                                var contentHtml = '';
                                if (table == 'invest') {
                                    $("#table-list").append('<li class="layui-this">投入品记录</li>');
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                } else if (table == 'check') {
                                    $("#table-list").append('<li class="layui-this">检查记录</li>');
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                } else if (table == 'machining') {
                                    $("#table-list").append('<li class="layui-this">加工记录</li>');
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                } else if (table == 'transport') {
                                    $("#table-list").append('<li class="layui-this">运输记录</li>');
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                } else {
                                    $.ajax({
                                        url: '/table/' + table,
                                        type: 'get',
                                        async: false,
                                        success: function (data) {
                                            $("#table-list").append('<li class="layui-this">'+ data.nameZh +'</li>');
                                        }
                                    });
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecord?tableName='+ table +'&id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                }
                                $("#table-content").append(contentHtml);
                            } else {
                                var contentHtml = '';
                                if (table == 'invest') {
                                    $("#table-list").append('<li class="">投入品记录</li>');
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                } else if (table == 'check') {
                                    $("#table-list").append('<li class="">检查记录</li>');
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                } else if (table == 'machining') {
                                    $("#table-list").append('<li class="">加工记录</li>');
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                } else if (table == 'transport') {
                                    $("#table-list").append('<li class="">运输记录</li>');
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                } else {
                                    $.ajax({
                                        url: '/table/' + table,
                                        type: 'get',
                                        async: false,
                                        success: function (data) {
                                            $("#table-list").append('<li class="">'+ data.nameZh +'</li>');
                                        }
                                    });
                                    contentHtml = '<div class="layui-tab-item layui-show">' +
                                        '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecord?tableName='+ table +'&id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                                        '</div>';
                                }
                                $("#table-content").append(contentHtml);
                            }
                        });

                        //添加默认环境监控设备设备
                        $("#table-list").append('<li class="">环境监控数据</li>');
                        var contentHtml = '<div class="layui-tab-item">' +
                            '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecordMonitor?id=' + request.id + '&processId='+ ele.id +'" frameborder="0"></iframe>' +
                            '</div>';
                        $("#table-content").append(contentHtml);
                    }
                }
                lihtml += '</dl></li>';
                $("#process-tree").append(lihtml);
            });
            var innerHtml = '<span class="layui-nav-bar" style="height: 0px; top: 317.5px; opacity: 0;"></span>';
            $("#process-tree").append(innerHtml);
            //重新渲染导航元素
            layui.use('element', function () {
                var element = layui.element;
                var layFilter = $("#process-tree").attr('lay-filter');
                element.render('nav', layFilter);
            });
            $("#" + request.selected).css("color","#1E9FFF");
            $("#" + request.selected).find('span').css({"color":"#1E9FFF"});

            //绑定事件
            $("#process-tree li").click(function () {
                if ($(this).hasClass('layui-nav-itemed')) {
                    $(this).addClass('layui-nav-itemed');
                } else {
                    $(this).removeClass('layui-nav-itemed');
                }
            });
            //点击tab导航刷新表格
            $("#table-list li").click(function () {
                var iframe = $("#table-content .layui-tab-item").eq($(this).index()).find('iframe');
                var url = iframe.attr("src");
                iframe.attr("src",url);
            });
        });
    } else { //二次加载
        $.get("/process/tree/" + request.processTemplateId,function (data) {
            $.each(data.data,function (i,ele) {
                var lihtml = '<li class="layui-nav-item layui-nav-itemed">' +
                    '<a id="'+ ele.id +'" href="/admin/traceability/record/recordList2?selected='+ ele.id +'&id=' + request.id + '&processTemplateId=' + request.processTemplateId + '&tableType='+ ele.tableType +'"><span>'+ ele.name +'</span></a>';
                //如果存在二级分类
                if (ele.children != null && ele.children.length != 0) {
                    lihtml = '<li class="layui-nav-item layui-nav-itemed">' +
                        '<a href="javascript:;"><span>'+ ele.name +'</span></a><dl class="layui-nav-child">';
                    $.each(ele.children,function (j, ele) {
                        lihtml += '<dd class=""><a id="'+ ele.id +'"  href="/admin/traceability/record/recordList2?selected='+ ele.id +'&id=' + request.id + '&processTemplateId=' + request.processTemplateId + '&tableType='+ ele.tableType +'"><span class="span-width"></span>'+ ele.name +'</a></dd>';
                    });
                }
                lihtml += '</dl></li>';
                $("#process-tree").append(lihtml);
            });

            //加载表格类型
            $.get("/process/" + request.selected,function (data) {
                $("#table-list").html('');
                $("#table-content").html('');
                var tableArr = data.tableType.split(",");
                $.each(tableArr,function (index, table) {
                    if (index === 0) {
                        var contentHtml = '';
                        if (table == 'invest') {
                            $("#table-list").append('<li class="layui-this">投入品记录</li>');
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        } else if (table == 'check') {
                            $("#table-list").append('<li class="layui-this">检查记录</li>');
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        } else if (table == 'machining') {
                            $("#table-list").append('<li class="layui-this">加工记录</li>');
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        } else if (table == 'transport') {
                            $("#table-list").append('<li class="layui-this">运输记录</li>');
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        } else {
                            $.ajax({
                                url: '/table/' + table,
                                type: 'get',
                                async: false,
                                success: function (data) {
                                    $("#table-list").append('<li class="layui-this">'+ data.nameZh +'</li>');
                                }
                            });
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecord?tableName='+ table +'&id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        }
                        $("#table-content").append(contentHtml);
                    } else {
                        var contentHtml = '';
                        if (table == 'invest') {
                            $("#table-list").append('<li class="">投入品记录</li>');
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        } else if (table == 'check') {
                            $("#table-list").append('<li class="">检查记录</li>');
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        } else if (table == 'machining') {
                            $("#table-list").append('<li class="">加工记录</li>');
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        } else if (table == 'transport') {
                            $("#table-list").append('<li class="">运输记录</li>');
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/'+ table +'?id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        } else {
                            $.ajax({
                                url: '/table/' + table,
                                type: 'get',
                                async: false,
                                success: function (data) {
                                    $("#table-list").append('<li class="">'+ data.nameZh +'</li>');
                                }
                            });
                            contentHtml = '<div class="layui-tab-item layui-show">' +
                                '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecord?tableName='+ table +'&id=' + request.id + '&processId='+  data.id +'" frameborder="0"></iframe>' +
                                '</div>';
                        }
                        $("#table-content").append(contentHtml);
                    }

                });

                //添加默认环境监控设备设备
                $("#table-list").append('<li class="">环境监控数据</li>');
                var contentHtml = '<div class="layui-tab-item">' +
                    '                <iframe id="colChanelList" src="/admin/traceability/record/tacerecord/tracerecordMonitor?id=' + request.id + '&processId='+ data.id +'" frameborder="0"></iframe>' +
                    '</div>';
                $("#table-content").append(contentHtml);

                //点击tab导航刷新表格
                $("#table-list li").click(function () {
                    var iframe = $("#table-content .layui-tab-item").eq($(this).index()).find('iframe');
                    var url = iframe.attr("src");
                    iframe.attr("src",url);
                });
            });

            var innerHtml = '<span class="layui-nav-bar" style="height: 0px; top: 317.5px; opacity: 0;"></span>';
            $("#process-tree").append(innerHtml);
            //重新渲染导航元素
            layui.use('element', function () {
                var element = layui.element;
                var layFilter = $("#process-tree").attr('lay-filter');
                element.render('nav', layFilter);
            });
            $("#" + request.selected).css({"color":"#1E9FFF"});
            $("#" + request.selected).find('span').css({"color":"#1E9FFF"});

            //绑定事件
            $("#process-tree li").click(function () {
                if ($(this).hasClass('layui-nav-itemed')) {
                    $(this).addClass('layui-nav-itemed');
                } else {
                    $(this).removeClass('layui-nav-itemed');
                }
            });

        });
    }


    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});